/*
 * =====================================================================================
 *
 *       Filename:  glasswindow.cc
 *
 *    Description:  Glass Window实现
 *
 *        Version:  1.0
 *        Created:  12/01/11 20:44:01
 *       Revision:  none
 *       Compiler:  gcc
 *
 *         Author:  H_Ley (), kalxd@126.com
 *        Company:  
 *
 * =====================================================================================
 */

#include "glasswindow.h"

GlassWindow::GlassWindow(QWidget *parent):QWidget(parent){
	background_color_alpha=1.0;

	QString button_styles=""
		"QPushButton{border:none;background:transparent;image:url(images/glassclosebutton_normal.png);}"
		"QPushButton:hover{image:url(images/glassclosebutton_hover.png);}"
		"QPushButton:focus{image:url(images/glassclosebutton_focus.png);}";
	close_button=new QPushButton(this);
	close_button->setFocusPolicy(Qt::NoFocus);
	close_button->resize(30,20);
	close_button->setText(tr(""));
	close_button->setStyleSheet(button_styles);
	connect(close_button,SIGNAL(clicked()),SLOT(close()));

	bg_pix=new QPixmap;
	bg_pix->load("images/glassbackground.png");

	bg_color.setRgb(255,255,255);
	bg_color.setAlpha(1.0);

	setAttribute(Qt::WA_TranslucentBackground,true);
	setWindowFlags(Qt::FramelessWindowHint);
}

GlassWindow::~GlassWindow(){
	delete bg_pix;
}

void GlassWindow::paintEvent(QPaintEvent *event){
	QRect sz=geometry();

	QPainter painter(this);
	painter.fillRect(sz,bg_color);
	*bg_pix=bg_pix->scaled(sz.width(),sz.height(),Qt::IgnoreAspectRatio,Qt::SmoothTransformation);
	painter.drawPixmap(0,0,*bg_pix);

	painter.end();
}

void GlassWindow::mousePressEvent(QMouseEvent *event){
	old_point=event->globalPos();
}

void GlassWindow::mouseMoveEvent(QMouseEvent *event){
	QRect sz=geometry();
	if(event->buttons()==Qt::LeftButton){
		if(event->pos().y()>-1 && event->pos().y()<30
				&& event->pos().x()>-1 && event->pos().x()<sz.width()){
			QPoint new_point=event->globalPos();
			QPoint to_move_point=mapToParent(new_point-old_point);
			move(to_move_point);
			old_point=new_point;
		}
	}
}

void GlassWindow::resizeEvent(QResizeEvent *event){
	QRect sz=geometry();

	close_button->move(sz.width()-45,1);
}
